<?php
declare(strict_types=1);

namespace think\admin\test\model;

use think\admin\Model;
use think\model\relation\BelongsToMany;

class User extends Model
{
    /**
     * 数据表名称
     * @var string
     */
    protected $table = 'user';

    /**
     * 允许写入的字段
     * @var array
     */
    protected $allowField = ['username', 'password', 'email', 'status'];

    /**
     * 隐藏的字段
     * @var array
     */
    protected $hidden = ['password'];

    /**
     * 获取用户角色关联
     * @return BelongsToMany
     */
    public function roles(): BelongsToMany
    {
        return $this->belongsToMany(Role::class, 'user_role', 'user_id', 'role_id');
    }

    /**
     * 密码修改器
     * @param string $value
     * @return string
     */
    public function setPasswordAttr(string $value): string
    {
        return password_hash($value, PASSWORD_DEFAULT);
    }

    /**
     * 验证密码
     * @param string $password
     * @return bool
     */
    public function verifyPassword(string $password): bool
    {
        return password_verify($password, $this->password);
    }
} 